package com.situ.fly.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.situ.fly.entity.Sign;

@Mapper
public interface SignMapper {

	/**
	 * 根据ID获取信息
	 */
	@Select("select * from sign where uid=#{uid}")
	Sign selectByUid(Integer uid);
	
	/**
	 * 插入信息
	 */
	@Insert("insert into sign (uid, count) value (#{uid}, 1)")
	int insert(Integer uid);
	
	/**
	 * 更新签到信息
	 */
	@Update("update sign set count=#{count}, lasttime=CURRENT_TIMESTAMP where id=#{id}")
	int update(Sign sign);

	/**
	 * 最近的签到
	 * @return
	 */
	@Select("select `user`.nickname, `user`.head, sign.* from user, sign " + 
			"where `user`.id = sign.uid and TO_DAYS(lasttime) = TO_DAYS(NOW()) " + 
			"order by lasttime desc")
	List<Map<String, Object>> selectOrderByTimeDesc();
	

	/**
	 * 最快的签到
	 * @return
	 */
	@Select("select `user`.nickname, `user`.head, sign.* from user, sign " + 
			"where `user`.id = sign.uid and TO_DAYS(lasttime) = TO_DAYS(NOW()) " + 
			"order by lasttime")
	List<Map<String, Object>> selectOrderByTime();

	/**
	 * 连续最多的
	 * @return
	 */
	@Select("select `user`.nickname, `user`.head, sign.* from user, sign " + 
			"where `user`.id = sign.uid and TO_DAYS(lasttime) = TO_DAYS(NOW()) " + 
			"order by `count` desc")
	List<Map<String, Object>> selectOrderByCountDesc();
	
}
